package com.rc.saas.tenant.service.system;

import com.rc.saas.tenant.mapper.system.RcMsgNotificationMapper;
import com.rc.saas.tenant.model.system.RcMsgNotification;
import com.rc.saas.tenant.model.system.RcMsgNotificationExample;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class RcMsgNotificationServiceImpl implements RcMsgNotificationService {

    @Autowired
    private RcMsgNotificationMapper rcMsgNotificationMapper;

    @Override
    public RcMsgNotificationMapper getMapper() {
        return rcMsgNotificationMapper;
    }

    @Override
    public List<RcMsgNotification> listRcMsgNotification(String tenantCode) {
        RcMsgNotificationExample example = new RcMsgNotificationExample();
        example.createCriteria().andTenantCodeEqualTo(tenantCode).andIsDeteleEqualTo("0");
        example.setOrderByClause("msg_notification_id desc,is_read desc");
        List<RcMsgNotification> list = rcMsgNotificationMapper.selectByExample(example);
        return list;
    }

    @Override
    public String updateMsgForRead(Long msgId) {
        RcMsgNotification msgNotification = rcMsgNotificationMapper.selectByPrimaryKey(msgId);
        msgNotification.setIsRead("1");
        rcMsgNotificationMapper.updateByPrimaryKey(msgNotification);
        return msgNotification.getMsgType();
    }

    @Override
    public void updateMsgForClearMsg(String tenantCode) {
        RcMsgNotificationExample example = new RcMsgNotificationExample();
        example.createCriteria().andIsDeteleEqualTo("0").andTenantCodeEqualTo(tenantCode);
        RcMsgNotification record = new RcMsgNotification();
        record.setIsDetele("1");
        record.setLastUpdatedTime(new Date());
        rcMsgNotificationMapper.updateByExampleSelective(record, example);
    }
}
